package com.vcredit.creditcat.manager;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.opensymphony.oscache.util.StringUtil;
import com.vcredit.creditcat.api.ICustManager;
import com.vcredit.creditcat.dto.feedback.FeedBackDto;
import com.vcredit.creditcat.exception.DataException;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.model.Feedback;
import com.vcredit.creditcat.service.CustService;
import com.vcredit.creditcat.service.FeedbackService;
import com.vcredit.creditcat.util.Utility;
import com.vcredit.framework.util.EmojiUtil;

@Service(protocol = {"protocol1"}, version = "1.0")
public class CustManager implements ICustManager {

  @Autowired
  private CustService custService;
  @Autowired
  private FeedbackService feedbackService;

  @Override
  public Cust findById(Long custId) {
    return custService.findById(custId);
  }

  /** 
   * TODO 简单描述该方法的实现功能（可选）. 
   * @throws DataException 
   * @see com.vcredit.creditcat.api.ICustManager#saveFeedback(java.lang.Long, com.vcredit.creditcat.feedback.FeedBackDto) 
   */
  @Override
  public void saveFeedback(Long custId, FeedBackDto paramDto) throws DataException {
    Cust cust = custService.findById(custId);
    String contactInfo = paramDto.getContactInfo();
    String commentText = paramDto.getCommentText();
    commentText = EmojiUtil.filterEmoji(Utility.clearBlank(commentText));
    if (StringUtil.hasLength(contactInfo) && !contactInfo.matches("^1\\d{10}$") //联系信息（邮箱或手机号）
        && !contactInfo.matches("\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}")) {
      throw new DataException();
    }
    Feedback feedback =
        new Feedback(
          custId, 
          cust.getCustName(), 
          contactInfo, 
          paramDto.getCommentKind(),
          commentText,
          0,
          paramDto.getDeviceDesc());
    feedbackService.insert(feedback);
  }
}
